﻿#region LICENSE
// ------------------------------------------------------------------------------------------------------------------------------------
//    Project: https://code.google.com/p/comlib/
//    File: Helper.cs
//    Copyright (C) 2003 Sergy Laman and other authors

//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.

//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.

//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
// ------------------------------------------------------------------------------------------------------------------------------------
#endregion
using System;
using System.Xml.Serialization;
using System.IO;

namespace QickSoft.Common
{
    public class Cast
    {
        public Cast()
        {
        }
        /// <summary>
        /// common functions
        /// </summary>
        public static string ToEmpty(string obj)
        {
            if (obj == null) return string.Empty;
            return obj;
        }
        public static string ToString(object obj)
        {
            if (obj == null || obj == DBNull.Value) return string.Empty;
            return obj.ToString().Trim();
        }
        public static string ToString(object obj, string defaultValue)
        {
            if (obj == null || obj == DBNull.Value) return defaultValue;
            string s = obj.ToString().Trim();
            if (s == "") return defaultValue;
            return s;
        }
        public static string ToLower(object obj)
        {
            if (obj == null || obj == DBNull.Value) return string.Empty;
            return obj.ToString().Trim().ToLower();
        }
        public static string ToUpper(object obj)
        {
            if (obj == null || obj == DBNull.Value) return string.Empty;
            return obj.ToString().Trim().ToUpper();
        }
        public static long ToLong(object obj)
        {
            long value = 0;
            if (obj == null || obj == DBNull.Value) return value;
            long.TryParse(obj.ToString(), out value);
            return value;
        }
        public static long ToLong(object obj, long def)
        {
            if (obj == null || obj == DBNull.Value) return def;
            long.TryParse(obj.ToString(), out def);
            return def;
        }

        public static int ToInt(object obj)
        {
            decimal value = 0;
            if (obj == null || obj == DBNull.Value) return (int)value;
            if (!Decimal.TryParse(obj.ToString(), out value))
            {
                if (obj.ToString().ToLower() == "true") return 1;
                if (obj.ToString().ToLower() == "false") return 0;
            }
            return (int)Math.Round(value);
        }
        public static int ToInt(object obj, int def)
        {
            int value = def;
            if (obj == null || obj == DBNull.Value) return value;
            int.TryParse(obj.ToString(), out value);
            return value;
        }
        public static double ToDouble(object obj)
        {
            double value = 0;
            if (obj == null || obj == DBNull.Value) return value;
            double.TryParse(obj.ToString(), out value);
            if (double.IsNaN(value)) return 0;
            return value;
        }
        public static double ToDouble(object obj, double def)
        {
            if (obj == null || obj == DBNull.Value) return def;
            double.TryParse(obj.ToString(), out def);
            if (double.IsNaN(def)) return 0;
            return def;
        }
        public static double ToDouble(object obj, int digitsToRound)
        {
            double value = 0;
            if (obj == null || obj == DBNull.Value) return value;
            double.TryParse(obj.ToString(), out value);
            if (double.IsNaN(value)) return 0;
            value = Math.Round(value, digitsToRound);
            return value;
        }
        public static DateTime ToDate(object obj)
        {
            DateTime value = DateTime.MinValue;
            if (obj == null || obj == DBNull.Value) return value;
            DateTime.TryParse(obj.ToString(), out value);
            return value;
        }

        public static bool ToBool(object obj)
        {
            bool value = false;
            if (obj == null || obj == DBNull.Value) return value;
            if (obj.ToString() == "1") return true;
            if (obj.ToString().ToUpper() == ".T.") return true;
            if (obj.ToString().ToUpper() == ".F.") return false;
            Boolean.TryParse(obj.ToString(), out value);
            return value;
        }

        public static Guid ToGuid(object p)
        {
            Guid g = new Guid("00000000-0000-0000-0000-000000000000");
            if (p == null || p == DBNull.Value || (string)p == "") return g;
            try
            {
                g = new Guid(p.ToString());
            }
            catch (Exception) { };
            return g;
        }



        internal static string ToSafeSqlString(object obj)
        {
            if (obj == null || obj == DBNull.Value) return string.Empty;
            if (obj is DateTime)
            {
                DateTime datetime = (DateTime)obj;
                return datetime.Year + "-" + datetime.Month + "-" + datetime.Day + " " +
                    datetime.Hour + ":" + datetime.Minute + ":" + datetime.Second;
            }
            return obj.ToString().Trim().Replace("'", "''");
        }

        /// <summary>
        /// Capitalize first letter of a word
        /// </summary>
        internal static string ToProper(object p)
        {
            string s = ToLower(p);
            if (s.Length == 0) return s;
            return s.Substring(0, 1).ToUpper() + s.Substring(1, s.Length - 1);
        }

        /// <summary>
        /// Converts to type
        /// </summary>
        public static object ToType(Type type, object o)
        {
            if (o == DBNull.Value) return o;//skip cast just return dbnull
            if (typeof(bool) == type) return Cast.ToBool(o);
            if (typeof(DateTime) == type) return Cast.ToDate(o);
            if (typeof(Int16) == type || typeof(Int32) == type || typeof(Decimal) == type) return Cast.ToInt(o);
            if (typeof(double) == type) return Cast.ToDouble(o);
            return o;
        }


        internal static string ToXml<T>(T t)
        {
            XmlSerializer sr;
            StringWriter srw;
            /*if (t is DataSet)
            {
                sr = new XmlSerializer(typeof(DataSet));
                srw = new StringWriter();
                sr.Serialize(srw, t as DataSet);
                return srw.ToString();
            }*/
            sr = new XmlSerializer(typeof(T));
            srw = new StringWriter();
            sr.Serialize(srw, t);
            return srw.ToString();
        }

    }

}

